#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "authorization.h"
#include "qsql.h"
#include <qsqltablemodel.h>
#include <QDebug>
#include "settings.h"

MainWindow::MainWindow(QWidget *parent) :   QMainWindow(parent),    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->tableWidget, SIGNAL(cellPressed(int,int)), SLOT(handleCellActivated(int, int)));
    connect(ui->orderTable, SIGNAL(cellPressed(int,int)), SLOT(handleOrderCellActivated(int, int)));
    connect(ui->shortInfoTable, SIGNAL(cellPressed(int,int)), SLOT(handleGoodsCellActivated(int, int)));
    connect(ui->declTable, SIGNAL(cellPressed(int,int)), SLOT(handleDeclCellActivated(int, int)));

    connect(ui->senderName, SIGNAL(textEdited(QString)), SLOT (on_senderName_textEdited(const QString)));
    connect(ui->senderAddress, SIGNAL(textEdited(QString)), SLOT (on_senderAddress_textEdited(const QString)));
    connect(ui->senderCountry, SIGNAL(textEdited(QString)), SLOT (on_senderCountry_textEdited(const QString)));
//    connect(ui->senderCounClass, SIGNAL(textEdited(QString)), SLOT (on_senderCounClass_textEdited(const QString)));

    connect(ui->destinName, SIGNAL(textEdited(QString)), SLOT (on_destinName_textEdited(const QString)));
    connect(ui->destinInn, SIGNAL(textEdited(QString)), SLOT (on_destinInn_textEdited(const QString)));
    connect(ui->destinKpp, SIGNAL(textEdited(QString)), SLOT (on_destinKpp_textEdited(const QString)));
    connect(ui->destinOgrn, SIGNAL(textEdited(QString)), SLOT (on_destinOgrn_textEdited(const QString)));
    connect(ui->destinAddress, SIGNAL(textEdited(QString)), SLOT (on_destinAddress_textEdited(const QString)));

    connect(ui->contract, SIGNAL(textEdited(QString)), SLOT (on_contract_textEdited(const QString)));
    connect(ui->contractFrom, SIGNAL(textEdited(QString)), SLOT (on_contractFrom_textEdited(const QString)));
    connect(ui->contractTo, SIGNAL(textEdited(QString)), SLOT (on_contractTo_textEdited(const QString)));
    connect(ui->specification, SIGNAL(textEdited(QString)), SLOT (on_specification_textEdited(const QString)));
    connect(ui->specificationFrom, SIGNAL(textEdited(QString)), SLOT (on_specificationFrom_textEdited(const QString)));
    connect(ui->passport, SIGNAL(textEdited(QString)), SLOT (on_passport_textEdited(const QString)));

//    connect(ui->goodsNumber, SIGNAL(textEdited(QString)), SLOT (on_goodsNumber_textEdited(const QString)));
    connect(ui->ntwt, SIGNAL(textEdited(QString)), SLOT (on_ntwt_textEdited(const QString)));
    connect(ui->grwt, SIGNAL(textEdited(QString)), SLOT (on_grwt_textEdited(const QString)));
    connect(ui->placeNumber, SIGNAL(textEdited(QString)), SLOT (on_placeNumber_textEdited(const QString)));
    connect(ui->fullwt, SIGNAL(textEdited(QString)), SLOT (on_fullwt_textEdited(const QString)));

    connect(ui->deliveryTerms, SIGNAL(textEdited(QString)), SLOT (on_deliveryTerms_textEdited(const QString)));
//    connect(ui->termsCombo, SIGNAL(textEdited(QString)), SLOT (on_termsCombo_textEdited(const QString)));
    connect(ui->invoicePrice, SIGNAL(textEdited(QString)), SLOT (on_invoicePrice_textEdited(const QString)));
//    connect(ui->invCurr, SIGNAL(textEdited(QString)), SLOT (on_invCurr_textEdited(const QString)));
    connect(ui->trCountry, SIGNAL(textEdited(QString)), SLOT (on_trCountry_textEdited(const QString)));
//    connect(ui->trCClass, SIGNAL(textEdited(QString)), SLOT (on_trCClass_textEdited(const QString)));






    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->orderTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->orderTable->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->shortInfoTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->shortInfoTable->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->declTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->declTable->setSelectionBehavior(QAbstractItemView::SelectRows);




    ui->widget->hide();

    on_ConnectWithDataBase_triggered();

    updateStatus = new QLabel(this);
    updateStatus->setText("Status bar");

    statusBar()->addWidget(updateStatus);
    //ui->orderTable->setColumnHidden(0, true);
    ui->contactList->setColumnHidden(0, true);

    ui->extraPactTable->setColumnHidden(0, true);
    ui->transCostsTable->setColumnHidden(0, true);
    ui->preBorderTable->setColumnHidden(0, true);
    ui->preDepartTable->setColumnHidden(0, true);

    //ui->openFileButton->setStyleSheet("border:0px; background-image: url(:/graphics/resource/graphics/icon.gif);");
    updateFrame();
}

void MainWindow::updateFrame()
{
    //qDebug() << "ui->tabWidget->currentIndex()" << ui->tabWidget->currentIndex();
    switch(ui->tabWidget->currentIndex())
    {
    case 0: //методы клиента*/
        setClientInfo();
        setClientFullInfo();
        setCurrentContactInfo();
        break;
    case 1: //методы заявки
        setOrderInfo();
        //
        setCurrentOrderInfo();
        setCurrentGoodsInfo();
        //setCurrentFileInfo();
        //setGoodsList();
        break;
    case 2: //формирование деклораций
        setDeclorationList();
        setCurrentGoodsList();
        setAdditionalInfo();
        break;
    case 3: //методы суммарной инфы по деклорации
        if(currentDecl == "")
            ui->tab_4->setEnabled(false);
        else
            ui->tab_4->setEnabled(true);
        setCurrentDeclSumInfo();
        setCurrentDestInfo();
        setCurrentTsInfo();
        setCurrentTransInfo();
        setCurrentPactsInfo();
        qDebug()<< "current decl: " << currentDecl;
        break;
    case 4:
        setGoodsOfList();
        setCurrGoodsInfo();
        break;
    }
    repaint();
}
void MainWindow::setClientFullInfo()
{
    if(myDb.isOpen())
    {
        ui->_bank->setStyleSheet("background-color: #ffffff");
        ui->_cpp->setStyleSheet("background-color: #ffffff");
        ui->_curAccount->setStyleSheet("background-color: #ffffff");
        ui->_inn->setStyleSheet("background-color: #ffffff");
        ui->_jurAdd->setStyleSheet("background-color: #ffffff");
        ui->_ogrn->setStyleSheet("background-color: #ffffff");
        ui->_orgName->setStyleSheet("background-color: #ffffff");
        ui->_postAdd->setStyleSheet("background-color: #ffffff");
        ui->_prefix->setStyleSheet("background-color: #ffffff");
        ui->tableWidget->setStyleSheet("background-color: #ffffff");


        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getClientInfo(currentClient);
        //qDebug () << "queClientInfo->value(8).toString()" <<  queClientInfo->value(8).toString();
        while(queClientInfo->next())
        {
            ui->_bank->setText(queClientInfo->value(7).toString());
            ui->_cpp->setText(queClientInfo->value(2).toString());
            ui->_curAccount->setText(queClientInfo->value(6).toString());
            ui->_inn->setText(queClientInfo->value(1).toString());

            ui->_jurAdd->blockSignals(true);
            ui->_jurAdd->setText(queClientInfo->value(4).toString());
            ui->_jurAdd->blockSignals(false);

            ui->_ogrn->setText(queClientInfo->value(3).toString());
            ui->_orgName->setText(queClientInfo->value(0).toString());

            ui->_postAdd->blockSignals(true);
            ui->_postAdd->setText(queClientInfo->value(5).toString());
            ui->_postAdd->blockSignals(false);

            ui->_prefix->setText(queClientInfo->value(8).toString());
            ui->tableWidget->selectRow(currentClientRow);
        }
    }
    else
    {
        ui->textBrowser->show();
        ui->widget->hide();
        ui->_bank->setText("#####");
        ui->_cpp->setText("#####");
        ui->_curAccount->setText("#####");
        ui->_inn->setText("#####");
        ui->_jurAdd->blockSignals(true);
        ui->_jurAdd->setText("#####");
        ui->_jurAdd->blockSignals(false);
        ui->_ogrn->setText("#####");
        ui->_orgName->setText("#####");
        ui->_postAdd->blockSignals(true);
        ui->_postAdd->setText("#####");
        ui->_postAdd->blockSignals(false);
        ui->_prefix->setText("#####");
    }
}
QMap<QString, QString> MainWindow::getCurrentClientInfo()
{
    QMap<QString,QString> currentClientInformation;

    currentClientInformation["PREFIX"] = ui->_prefix->text();
    currentClientInformation["ORGNAME"] = ui->_orgName->text();
    currentClientInformation["INN"] = ui->_inn->text();
    currentClientInformation["KPP"] = ui->_cpp->text();
    currentClientInformation["OGRN"] = ui->_ogrn->text();
    currentClientInformation["JURADDRESS"] = ui->_jurAdd->toPlainText();
    currentClientInformation["POSTADDRESS"] = ui->_postAdd->toPlainText();
    currentClientInformation["CURRACCOUNT"] = ui->_curAccount->text();
    currentClientInformation["BANK"] = ui->_bank->text();



    return currentClientInformation;
}


void MainWindow::setCurrentContactInfo()
{
    QSqlQuery *queContactInfo = new QSqlQuery;
    queContactInfo = myDb.getContactInfo(currentClient);
    QTableWidgetItem *item;
    ui->contactList->setRowCount(0);
    ui->contactList->blockSignals(true);
    while(queContactInfo->next())
    {
        ui->contactList->setRowCount(ui->contactList->rowCount() + 1);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(0).toString());
        ui->contactList->setItem(ui->contactList->rowCount()-1, 0,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(1).toString());
        ui->contactList->setItem(ui->contactList->rowCount()-1, 1,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(2).toString());
        ui->contactList->setItem(ui->contactList->rowCount()-1, 2,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(3).toString());
        ui->contactList->setItem(ui->contactList->rowCount()-1, 3,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(4).toString());
        ui->contactList->setItem(ui->contactList->rowCount()-1, 4,item);
    }
    ui->contactList->blockSignals(false);
}

void MainWindow::setCurrentGoodsInfo()
{
    if(myDb.isOpen())
    {
        ui->goodTitle->setStyleSheet("background-color: #ffffff");
        ui->tnVedCode->setStyleSheet("background-color: #ffffff");
        ui->country->setStyleSheet("background-color: #ffffff");
        ui->weightNetto->setStyleSheet("background-color: #ffffff");
        ui->weightBrutto->setStyleSheet("background-color: #ffffff");
        ui->comment->setStyleSheet("background-color: #ffffff");
//        ui->employee->setStyleSheet("background-color: #ffffff");
        ui->registPlace->setStyleSheet("background-color: #ffffff");
        ui->fileInfoTable->setStyleSheet("background-color: #ffffff");
        ui->placeExec->setStyleSheet("background-color: #ffffff");


        ui->shortInfoTable->selectRow(currentGoodsRow);


        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getCurrentGoodsInfo(currentGoods);

        clearGoodsInfo();
        while(queClientInfo->next())
        {
            ui->goodTitle->setText(queClientInfo->value(3).toString());
            ui->tnVedCode->setText(queClientInfo->value(4).toString());
            ui->country->setText(queClientInfo->value(5).toString());
            ui->weightNetto->setText(queClientInfo->value(6).toString());
            ui->weightBrutto->setText(queClientInfo->value(7).toString());
            ui->registPlace->setText(queClientInfo->value(9).toString());
            ui->postCondition->setCurrentIndex(queClientInfo->value(8).toInt());
            ui->comment->blockSignals(true);
            ui->comment->setText(queClientInfo->value(11).toString());
            ui->comment->blockSignals(false);
            ui->placeExec->setText(queClientInfo->value(10).toString());
            ui->shortInfoTable->selectRow(currentGoodsRow);
        }
    }
    else
    {
        ui->goodTitle->setText("");
        ui->tnVedCode->setText("");
        ui->country->setText("");
        ui->weightNetto->setText("");
        ui->weightBrutto->setText("");
        ui->registPlace->setText("");
        ui->postCondition->setCurrentIndex(0);
        ui->comment->blockSignals(true);
        ui->comment->setText("");
        ui->comment->blockSignals(false);
    }
}

void MainWindow::setGoodsOfList()
{
    QSqlQuery *queClientInfo = new QSqlQuery;
    queClientInfo = myDb.getOrderInfo(currentOrder);
    ui->goodsCombo->clear();
    while(queClientInfo->next())
    {
        ui->goodsCombo->addItem(queClientInfo->value(2).toString() + ". " + queClientInfo->value(3).toString());
    }


}

void MainWindow::setCurrGoodsInfo()
{

    if(myDb.isOpen())
    {
        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getCurrentGoodsInfo(currentOfGoods);
        while(queClientInfo->next())
        {
            ui->le4->setText(queClientInfo->value(4).toString());
            ui->le5->setText(queClientInfo->value(5).toString());
            ui->le6->setText(queClientInfo->value(6).toString());
            ui->le7->setText(queClientInfo->value(7).toString());
//            ui->le8->setText(queClientInfo->value(8).toString());
//            ui->le9->setText(queClientInfo->value(9).toString());
//            ui->le10->setText(queClientInfo->value(10).toString());
//            ui->le11->setText(queClientInfo->value(11).toString());
            ui->le12->setText(queClientInfo->value(12).toString());
//            ui->le13->setText(queClientInfo->value(13).toString());
            ui->le14->setText(queClientInfo->value(14).toString());
            ui->le15->setText(queClientInfo->value(15).toString());
            ui->le16->setText(queClientInfo->value(16).toString());
            ui->le17->setText(queClientInfo->value(17).toString());
            ui->le18->setText(queClientInfo->value(18).toString());
            ui->le19->setText(queClientInfo->value(19).toString());
            ui->le20->setText(queClientInfo->value(20).toString());
            ui->le21->setText(queClientInfo->value(21).toString());
//            ui->le22->setText(queClientInfo->value(22).toString());
            ui->le23->setText(queClientInfo->value(23).toString());
            ui->le24->setText(queClientInfo->value(24).toString());

        }
    }
}

void MainWindow::setCurrentOrderInfo()
{
    QSqlQuery *queOrderInfo = new QSqlQuery;
    queOrderInfo = myDb.getOrderInfo(currentOrder);
    QTableWidgetItem *item;
    ui->shortInfoTable->setRowCount(0);
    ui->shortInfoTable->blockSignals(true);
    while(queOrderInfo->next())
    {
        ui->shortInfoTable->setRowCount(ui->shortInfoTable->rowCount() + 1);

        item = new QTableWidgetItem;
        item->setText(queOrderInfo->value(2).toString());
        ui->shortInfoTable->setItem(ui->shortInfoTable->rowCount()-1, 0,item);

        item = new QTableWidgetItem;
        item->setText(queOrderInfo->value(3).toString());
        ui->shortInfoTable->setItem(ui->shortInfoTable->rowCount()-1, 1,item);

        item = new QTableWidgetItem;
        item->setText(queOrderInfo->value(9).toString());
        ui->shortInfoTable->setItem(ui->shortInfoTable->rowCount()-1, 2,item);


    }

    ui->shortInfoTable->blockSignals(false);
    ui->orderTable->selectRow(currentOrderRow);
}

void MainWindow::setCurrentDeclSumInfo()
{
    if(myDb.isOpen())
    {
        ui->senderName->setStyleSheet("background-color: #ffffff");
        ui->senderAddress->setStyleSheet("background-color: #ffffff");
        ui->senderCountry->setStyleSheet("background-color: #ffffff");
//        ui->senderCounClass->setStyleSheet("background-color: #ffffff");

        ui->destinName->setStyleSheet("background-color: #ffffff");
        ui->destinInn->setStyleSheet("background-color: #ffffff");
        ui->destinKpp->setStyleSheet("background-color: #ffffff");
        ui->destinOgrn->setStyleSheet("background-color: #ffffff");
        ui->destinAddress->setStyleSheet("background-color: #ffffff");

        ui->contract->setStyleSheet("background-color: #ffffff");
        ui->contractFrom->setStyleSheet("background-color: #ffffff");
        ui->contractTo->setStyleSheet("background-color: #ffffff");
        ui->specification->setStyleSheet("background-color: #ffffff");
        ui->specificationFrom->setStyleSheet("background-color: #ffffff");
        ui->passport->setStyleSheet("background-color: #ffffff");

        ui->goodsNumber->setStyleSheet("background-color: #ffffff");
        ui->ntwt->setStyleSheet("background-color: #ffffff");
        ui->grwt->setStyleSheet("background-color: #ffffff");
        ui->placeNumber->setStyleSheet("background-color: #ffffff");
        ui->fullwt->setStyleSheet("background-color: #ffffff");

        ui->deliveryTerms->setStyleSheet("background-color: #ffffff");
//        ui->termsCombo->setStyleSheet("background-color: #ffffff");
        ui->invoicePrice->setStyleSheet("background-color: #ffffff");
//        ui->invCurr->setStyleSheet("background-color: #ffffff");
        ui->trCountry->setStyleSheet("background-color: #ffffff");
//        ui->trCClass->setStyleSheet("background-color: #ffffff");

        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getCurrentDeclInfo(currentDecl);
        clearDeclInfo();
        ui->orderNumber->setText("<html><head/><body><p><span style=' font-size:14pt;'>Заявка № " + currentOrder + " </span></p></body></html>");
        setCurrentDestInfo();
        while(queClientInfo->next())
        {
            ui->senderName->setText(queClientInfo->value(2).toString());
            ui->senderAddress->setText(queClientInfo->value(3).toString());
            ui->senderCountry->setText(queClientInfo->value(4).toString());
            ui->senderCounClass->setCurrentIndex(queClientInfo->value(5).toInt());


//            ui->destinName->setText(queClientInfo->value(6).toString());
//            ui->destinInn->setText(queClientInfo->value(7).toString());
//            ui->destinKpp->setText(queClientInfo->value(8).toString());
//            ui->destinOgrn->setText(queClientInfo->value(8).toString());
//            ui->destinAddress->setText(queClientInfo->value(9).toString());

            ui->contract->setText(queClientInfo->value(6).toString());
            ui->contractFrom->setText(queClientInfo->value(7).toString());
            ui->contractTo->setText(queClientInfo->value(8).toString());
            ui->specification->setText(queClientInfo->value(9).toString());
            ui->specificationFrom->setText(queClientInfo->value(10).toString());
            ui->passport->setText(queClientInfo->value(11).toString());

            ui->goodsNumber->setText(queClientInfo->value(12).toString());
            ui->ntwt->setText(queClientInfo->value(13).toString());
            ui->grwt->setText(queClientInfo->value(14).toString());
            ui->placeNumber->setText(queClientInfo->value(15).toString());
            ui->fullwt->setText(queClientInfo->value(16).toString());

            ui->deliveryTerms->setText(queClientInfo->value(17).toString());
            ui->termsCombo->setCurrentIndex(queClientInfo->value(18).toInt());
            ui->invoicePrice->setText(queClientInfo->value(19).toString());
            ui->invCurr->setCurrentIndex(queClientInfo->value(20).toInt());
            ui->trCountry->setText(queClientInfo->value(21).toString());
            ui->trCClass->setCurrentIndex(queClientInfo->value(22).toInt());
            ui->procCombo->setCurrentIndex(queClientInfo->value(23).toInt());
            ui->goodsNumber->setText(queClientInfo->value(24).toString());
        }
    }
    else
    {
        ui->senderName->setText("");
        ui->senderAddress->setText("");
        ui->senderCountry->setText("");
        ui->senderCounClass->setCurrentIndex(0);

        ui->destinName->setText("");
        ui->destinInn->setText("");
        ui->destinKpp->setText("");
        ui->destinOgrn->setText("");
        ui->destinAddress->setText("");

        ui->contract->setText("");
        ui->contractFrom->setText("");
        ui->contractTo->setText("");
        ui->specification->setText("");
        ui->specificationFrom->setText("");
        ui->passport->setText("");

        ui->goodsNumber->setText("");
        ui->ntwt->setText("");
        ui->grwt->setText("");
        ui->placeNumber->setText("");
        ui->fullwt->setText("");

        ui->deliveryTerms->setText("");
        ui->termsCombo->setCurrentIndex(0);
        ui->procCombo->setCurrentIndex(0);
        ui->invoicePrice->setText("");
        ui->invCurr->setCurrentIndex(0);
        ui->trCountry->setText("");
        ui->trCClass->setCurrentIndex(0);
    }
}

void MainWindow::setCurrentDestInfo()
{
    QSqlQuery *queClientInfo = new QSqlQuery;
    qDebug() << "current order: " << currentOrder;
    QString currentPrefix = myDb.getCurrentPrefix(currentOrder);
    queClientInfo = myDb.getClientInfo(currentPrefix);
    qDebug() << "current prefix: " << currentPrefix;
    while(queClientInfo->next())
    {
            ui->destinName->setText(queClientInfo->value(0).toString());
            ui->destinInn->setText(queClientInfo->value(1).toString());
            ui->destinKpp->setText(queClientInfo->value(2).toString());
            ui->destinOgrn->setText(queClientInfo->value(3).toString());
            ui->destinAddress->setText(queClientInfo->value(4).toString());
    }

}

void MainWindow::setClientInfo()
{
    ui->tableWidget->setRowCount(0);
    if(myDb.isOpen())
    {
        ui->textBrowser->hide();
        ui->widget->show();
        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getClientInfo();
        QTableWidgetItem *item;
        while(queClientInfo->next())
        {
           ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);
           item = new QTableWidgetItem;
           item->setText(queClientInfo->value(8).toString());
           item->setTextAlignment(Qt::AlignCenter);
           ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 0, item);

           item = new QTableWidgetItem;
           item->setText(queClientInfo->value(0).toString());
           item->setTextAlignment(Qt::AlignCenter);
           ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 1, item);
        }
    }
    else
    {
        ui->textBrowser->show();
        ui->widget->hide();
        //qDebug() << "ui->tableWidget->rowCount(): " << ui->tableWidget->rowCount();
        ui->tableWidget->setRowCount(0);
    }
}

void MainWindow::setOrderInfo()
{
    ui->orderTable->setRowCount(0);
    if(myDb.isOpen())
    {
        //ui->textBrowser->hide();
        //ui->widget->show();
        QSqlQuery *queOrderInfo = new QSqlQuery;
        queOrderInfo = myDb.getOrderInfo();
        QTableWidgetItem *item;
        while(queOrderInfo->next())
        {
           ui->orderTable->setRowCount(ui->orderTable->rowCount() + 1);
           item = new QTableWidgetItem;
           item->setText(queOrderInfo->value(1).toString());
           item->setTextAlignment(Qt::AlignCenter);
           ui->orderTable->setItem(ui->orderTable->rowCount() - 1, 1, item);

           item = new QTableWidgetItem;
           item->setText(queOrderInfo->value(0).toString());
           item->setTextAlignment(Qt::AlignCenter);
           ui->orderTable->setItem(ui->orderTable->rowCount() - 1, 0, item);

           item = new QTableWidgetItem;
           item->setText(queOrderInfo->value(2).toString());
           item->setTextAlignment(Qt::AlignCenter);
           ui->orderTable->setItem(ui->orderTable->rowCount() - 1, 2, item);
        }
    }
    else
    {
        //ui->textBrowser->show();
        //ui->widget->hide();
        //qDebug() << "ui->tableWidget->rowCount(): " << ui->tableWidget->rowCount();
        //ui->tableWidget->setRowCount(0);
    }
}

void MainWindow::setAdditionalInfo()
{
    if(myDb.isOpen())
    {
        QString addInfo;
        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getClientInfo(currentClient);

        while(queClientInfo->next())
        {
            addInfo += "<H3>Клиент: <b>" + queClientInfo->value(8).toString() + "</b> "+ queClientInfo->value(0).toString() + "</H3><br>";
            addInfo += "ИНН: ";
            addInfo += queClientInfo->value(1).toString() + "<br>";
            addInfo += "КПП: " + queClientInfo->value(2).toString() + "<br>";
            addInfo += "ОГРН: " + queClientInfo->value(3).toString() + "<br>";
            addInfo += "Юридический адрес: " + queClientInfo->value(4).toString() + "<br>";
            addInfo += "Почтовый адрес: " + queClientInfo->value(5).toString() + "<br>";
            addInfo += "Расчетный счет: " + queClientInfo->value(6).toString() + "<br>";
            addInfo += "Банк: " + queClientInfo->value(7).toString() + "<br>";

        }
        ui->additionalInfo->setText(addInfo);
    }
}

void MainWindow::setDeclorationList()
{
    ui->declTable->setRowCount(0);
    if(myDb.isOpen())
    {
//        ui->textBrowser->hide();
//        ui->widget->show();
        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getDeclInfo(currentOrder);
        QTableWidgetItem *item;
        while(queClientInfo->next())
        {
           ui->declTable->setRowCount(ui->declTable->rowCount() + 1);
           item = new QTableWidgetItem;
           item->setText(queClientInfo->value(1).toString());
           item->setTextAlignment(Qt::AlignCenter);
           ui->declTable->setItem(ui->declTable->rowCount() - 1, 0, item);

           item = new QTableWidgetItem;
           item->setText(queClientInfo->value(2).toString());
           item->setTextAlignment(Qt::AlignCenter);
           ui->declTable->setItem(ui->declTable->rowCount() - 1, 1, item);
        }
        ui->declTable->selectRow(currentDeclRow);
    }
    else
    {
        ui->textBrowser->show();
        ui->widget->hide();
        //qDebug() << "ui->tableWidget->rowCount(): " << ui->tableWidget->rowCount();
        ui->tableWidget->setRowCount(0);
    }
}

void MainWindow::setCurrentGoodsList()
{
    vectGoods.clear();
    QLayout* layout = ui->scrollAreaWidgetContents->layout();
    while(QLayoutItem* item = layout->takeAt(0))
    {
        if(QWidget* widget = item->widget())
            delete widget;
        delete item;
    }
    if(myDb.isOpen())
    {
//        ui->textBrowser->hide();
//        ui->widget->show();
        QSqlQuery *queClientInfo = new QSqlQuery;
        queClientInfo = myDb.getOrderInfo(currentOrder);
        qDebug() << "currOrder: " << currentOrder;
        while(queClientInfo->next())
        {
            vectGoods.push_back(new QCheckBox(queClientInfo->value(2).toString() + ": "+ queClientInfo->value(3).toString()));
            if(queClientInfo->value(1) == "0")
                    vectGoods.last()->setEnabled(true);
            else
                vectGoods.last()->setEnabled(false);

            if(queClientInfo->value(1) == currentDecl)
            {
                vectGoods.last()->setEnabled(true);
                vectGoods.last()->setChecked(true);

            }
            ui->scrollAreaWidgetContents->layout()->addWidget(vectGoods.last());
        }
        ui->scrollAreaWidgetContents->layout()->addItem(new QSpacerItem(20,40,QSizePolicy::Minimum, QSizePolicy::Expanding));
    }
}

void MainWindow::setCurrentTsInfo()
{
    {
        QSqlQuery *queContactInfo = new QSqlQuery;
        queContactInfo = myDb.getBorderInfo(currentDecl);
        QTableWidgetItem *item;
        ui->preBorderTable->setRowCount(0);
        ui->preBorderTable->blockSignals(true);
        while(queContactInfo->next())
        {
            ui->preBorderTable->setRowCount(ui->preBorderTable->rowCount() + 1);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(0).toString());
            ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1, 0,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(1).toString());
            ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1, 1,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(2).toString());
            ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1, 2,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(3).toString());
            ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1, 3,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(4).toString());
            ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1, 4,item);
        }
        ui->preBorderTable->blockSignals(false);
    }
    {
        QSqlQuery *queContactInfo = new QSqlQuery;
        queContactInfo = myDb.getDepartInfo(currentDecl);
        QTableWidgetItem *item;
        ui->preDepartTable->setRowCount(0);
        ui->preDepartTable->blockSignals(true);
        while(queContactInfo->next())
        {
            ui->preDepartTable->setRowCount(ui->preDepartTable->rowCount() + 1);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(0).toString());
            ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1, 0,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(1).toString());
            ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1, 1,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(2).toString());
            ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1, 2,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(3).toString());
            ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1, 3,item);

            item = new QTableWidgetItem;
            item->setText(queContactInfo->value(4).toString());
            ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1, 4,item);
        }
        ui->preDepartTable->blockSignals(false);
    }
}

void MainWindow::setCurrentTransInfo()
{
    QSqlQuery *queContactInfo = new QSqlQuery;
    queContactInfo = myDb.getTransInfo(currentDecl);
    QTableWidgetItem *item;
    ui->transCostsTable->setRowCount(0);
    ui->transCostsTable->blockSignals(true);
    while(queContactInfo->next())
    {
        ui->transCostsTable->setRowCount(ui->transCostsTable->rowCount() + 1);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(0).toString());
        ui->transCostsTable->setItem(ui->transCostsTable->rowCount()-1, 0,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(1).toString());
        ui->transCostsTable->setItem(ui->transCostsTable->rowCount()-1, 1,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(2).toString());
        ui->transCostsTable->setItem(ui->transCostsTable->rowCount()-1, 2,item);

    }
    ui->transCostsTable->blockSignals(false);
}

void MainWindow::setCurrentPactsInfo()
{
    QSqlQuery *queContactInfo = new QSqlQuery;
    queContactInfo = myDb.getPactsInfo(currentDecl);
    QTableWidgetItem *item;
    ui->extraPactTable->setRowCount(0);
    ui->extraPactTable->blockSignals(true);
    while(queContactInfo->next())
    {
        ui->extraPactTable->setRowCount(ui->extraPactTable->rowCount() + 1);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(0).toString());
        ui->extraPactTable->setItem(ui->extraPactTable->rowCount()-1, 0,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(1).toString());
        ui->extraPactTable->setItem(ui->extraPactTable->rowCount()-1, 1,item);

        item = new QTableWidgetItem;
        item->setText(queContactInfo->value(2).toString());
        ui->extraPactTable->setItem(ui->extraPactTable->rowCount()-1, 2,item);

    }
    ui->extraPactTable->blockSignals(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED (event);
    QPainter* painter = new QPainter(this);

    painter->setRenderHint(QPainter::Antialiasing, true);

    painter->drawText(this->width()-135,37, "Статус соединения:");
    QPen pen;
    pen.setWidth(5);

    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    //qDebug() << "PAINT:" << myDb.isOpen();
    if(myDb.isOpen())
    {
        brush.setColor(Qt::darkGreen);
        pen.setColor(Qt::darkGreen);
    }
    else
    {
        brush.setColor(Qt::darkRed);
        pen.setColor(Qt::darkRed);
    }

    painter->setBrush(brush);
    painter->setPen(pen);
    painter->drawEllipse(this->width()-20,29,7,7);
}

void MainWindow::on_Exit_triggered()
{
    this->close();
}

void MainWindow::on_ConnectWithDataBase_triggered()
{
    if(!myDb.isOpen())
    {
        Authorization aut;
        aut.setModal(true);
        if(aut.exec() == QDialog::Accepted)
            {
                myDb.connectWithDataBase(aut.getLogin(),aut.getPassword());
                updateFrame();
            }
    }

}

void MainWindow::on_AddClient_triggered()
{
    AddClient addCl;
    addCl.setModal(false);
    if(addCl.exec() == QDialog::Accepted)
    {
        myDb.addClient(addCl.getClientInfo());
        updateFrame();

    }
}

void MainWindow::on_DisconnectWithDataBase_triggered()
{
    myDb.disconnectWithDataBase();
    updateFrame();
}


void MainWindow::handleOrderCellActivated(int row, int cell)
{
    Q_UNUSED (cell);
    currentOrder = ui->orderTable->item(row,1)->text();
//    ui->employee->setText(ui->orderTable->item(row,2)->text());
    currentOrderRow = row;

    clearGoodsInfo();
    currentGoods = "";
    currentDecl = "";
    currentDeclRow = 0;

    currentGoodsRow = 0;
    QString orderInfo;
    orderInfo = ui->orderTable->item(currentOrderRow, 1)->text() + "." + ui->orderTable->item(currentOrderRow, 0)->text() + "   Ответственный: " + ui->orderTable->item(currentOrderRow, 2)->text();
    ui->orderInfoLabel->setText(orderInfo);
    ui->orderInfoLabel->setStyleSheet("font-size: 14pt;");
    updateFrame();

}

void MainWindow::handleGoodsCellActivated(int row, int cell)
{
    Q_UNUSED (cell);
    currentGoods = ui->shortInfoTable->item(row,0)->text();
    qDebug() << "currentGoods" << currentGoods;
    currentGoodsRow = row;
    updateFrame();

}

void MainWindow::handleDeclCellActivated(int row, int cell)
{
    currentDecl = ui->declTable->item(row,0)->text();
    currentDeclRow = row;
    setAdditionalInfo();
    updateFrame();
}

void MainWindow::handleCellActivated(int row, int cell)
{
    Q_UNUSED (cell);
    currentClient = ui->tableWidget->item(row,0)->text();
    currentClientRow = row;
    updateFrame();

}

void MainWindow::updateClientWidget(int row)
{
    QString test;
    test += row;
}

void MainWindow::clearGoodsInfo()
{
    ui->goodTitle->setText("");
    ui->tnVedCode->setText("");
    ui->country->setText("");
    ui->weightNetto->setText("");
    ui->weightBrutto->setText("");
    ui->registPlace->setText("");
    ui->postCondition->setCurrentIndex(0);
    ui->comment->blockSignals(true);
    ui->comment->setText("");
    ui->comment->blockSignals(false);
    ui->placeExec->setText("");
//    currentGoods = "";
//    currentGoodsRow = NULL;

}

void MainWindow::clearDeclInfo()
{
    ui->senderName->setText("");
    ui->senderAddress->setText("");
    ui->senderCountry->setText("");
    ui->senderCounClass->setCurrentIndex(0);

    ui->destinName->setText("");
    ui->destinInn->setText("");
    ui->destinKpp->setText("");
    ui->destinOgrn->setText("");
    ui->destinAddress->setText("");

    ui->contract->setText("");
    ui->contractFrom->setText("");
    ui->contractTo->setText("");
    ui->specification->setText("");
    ui->specificationFrom->setText("");
    ui->passport->setText("");

    ui->goodsNumber->setText("");
    ui->ntwt->setText("");
    ui->grwt->setText("");
    ui->placeNumber->setText("");
    ui->fullwt->setText("");

    ui->deliveryTerms->setText("");
        ui->termsCombo->setCurrentIndex(0);
        ui->procCombo->setCurrentIndex(0);
    ui->invoicePrice->setText("");
        ui->invCurr->setCurrentIndex(0);
    ui->trCountry->setText("");
        ui->trCClass->setCurrentIndex(0);
}

QMap<QString,QString> MainWindow::getCurrentContactInfo(int id)
{
    QMap<QString,QString> currentContactInformation;

    currentContactInformation["ID"] = ui->contactList->item(id, 0)->text();
    currentContactInformation["FIO"] = ui->contactList->item(id, 1)->text();
    currentContactInformation["POSITION"] = ui->contactList->item(id, 2)->text();
    currentContactInformation["PHONE"] = ui->contactList->item(id, 3)->text();
    currentContactInformation["EMAIL"] = ui->contactList->item(id, 4)->text();
    currentContactInformation["PREFIX"] = ui->_prefix->text();

    return currentContactInformation;
}

QMap<QString, QString> MainWindow::getTransInfo(int id)
{
     QMap<QString,QString> currentTransInformation;

     currentTransInformation["DECLORATION_ID"] = currentDecl;
     currentTransInformation["COST"] = ui->transCostsTable->item(id, 1)->text();
     currentTransInformation["VAL"] = ui->transCostsTable->item(id, 2)->text();
     currentTransInformation["ID"] = ui->transCostsTable->item(id, 0)->text();

     return currentTransInformation;
}

QMap<QString, QString> MainWindow::getCurrentBorderInfo(int id)
{
    QMap<QString,QString> currentBInfo;

    currentBInfo["DECLORATION_ID"] = currentDecl;
    currentBInfo["TYPE"] = ui->preBorderTable->item(id, 1)->text();
    currentBInfo["VAL1"] = ui->preBorderTable->item(id, 2)->text();
    currentBInfo["VAL2"] = ui->preBorderTable->item(id, 3)->text();
    currentBInfo["CLASS"] = ui->preBorderTable->item(id, 4)->text();
    currentBInfo["ID"] = ui->preBorderTable->item(id, 0)->text();

    return currentBInfo;
}

QMap<QString, QString> MainWindow::getCurrentDepartInfo(int id)
{
    QMap<QString,QString> currentDInfo;

    currentDInfo["DECLORATION_ID"] = currentDecl;
    currentDInfo["TYPE"] = ui->preDepartTable->item(id, 1)->text();
    currentDInfo["VAL1"] = ui->preDepartTable->item(id, 2)->text();
    currentDInfo["VAL2"] = ui->preDepartTable->item(id, 3)->text();
    currentDInfo["CLASS"] = ui->preDepartTable->item(id, 4)->text();
    currentDInfo["ID"] = ui->preDepartTable->item(id, 0)->text();

    return currentDInfo;
}

QMap<QString, QString> MainWindow::getCurrentPactInfo(int id)
{
    QMap<QString,QString> currentPInfo;

    currentPInfo["DECLORATION_ID"] = currentDecl;
    currentPInfo["name"] = ui->extraPactTable->item(id, 1)->text();
    currentPInfo["date"] = ui->extraPactTable->item(id, 2)->text();
    currentPInfo["ID"] = ui->extraPactTable->item(id, 0)->text();

    return currentPInfo;
}

QMap<QString, QString> MainWindow::getCurrentOrderInfo(int id)
{
    QMap<QString,QString> currentOrderInformation;

    currentOrderInformation["DECLORATION_ID"] = ui->shortInfoTable->item(id, 0)->text();
    currentOrderInformation["ARTICLE_NAME"] = ui->shortInfoTable->item(id, 1)->text();
    currentOrderInformation["PLACE_DEL"] = ui->shortInfoTable->item(id, 2)->text();

    return currentOrderInformation;
}

QMap<QString, QString> MainWindow::getCurrentGoodsInfo()
{
    QMap<QString, QString> currentGoodsInfo;

    currentGoodsInfo["ARTICLE_NAME"] = ui->goodTitle->text();
    currentGoodsInfo["TNVED"] = ui->tnVedCode->text();
    currentGoodsInfo["PLACE_DEL"] = ui->registPlace->text();
    currentGoodsInfo["M_NETTO"] = ui->weightNetto->text();
    currentGoodsInfo["M_BRUTTO"] = ui->weightBrutto->text();
    currentGoodsInfo["COMMENT"] = ui->comment->toPlainText();
    currentGoodsInfo["DECLORATION_ID"] = currentGoods;
    currentGoodsInfo["PLACE_EXEC"] = ui->placeExec->text();
    currentGoodsInfo["CONDITION_DEL"] = QString().number(ui->postCondition->currentIndex(),10);


//    qDebug() << "currentGoodsInfo[CONDITION_DEL]: " <<  currentGoodsInfo["CONDITION_DEL"];
    return currentGoodsInfo;
}

QMap<QString, QString> MainWindow::getCurrentOfGoodsInfo()
{
    QMap<QString, QString> currentGoodsInfo;

               currentGoodsInfo["TNVED"] = ui->le4->text();
               currentGoodsInfo["COUNTRY"] = ui->le5->text();
               currentGoodsInfo["M_NETTO"] = ui->le6->text();
               currentGoodsInfo["M_BRUTTO"] = ui->le7->text();
               currentGoodsInfo["inform"] = ui->le12->toPlainText();
               currentGoodsInfo["wpallet"] = ui->le14->text();
               currentGoodsInfo["goodscount"] = ui->le15->text();
               currentGoodsInfo["placecount"] = ui->le16->text();
               currentGoodsInfo["packtype"] = ui->le17->text();
               currentGoodsInfo["indcount"] = ui->le18->text();
               currentGoodsInfo["indtype"] = ui->le19->text();
               currentGoodsInfo["price"] = ui->le20->text();
               currentGoodsInfo["posh"] = ui->le21->text();
               currentGoodsInfo["pref"] = ui->le23->toPlainText();
               currentGoodsInfo["prim"] = ui->le24->toPlainText();
               currentGoodsInfo["DECLORATION_ID"] = currentOfGoods;


    return currentGoodsInfo;
}

QMap<QString, QString> MainWindow::getCurrentDeclInfo()
{
    QMap<QString, QString> currentDeclInfo;

    currentDeclInfo["sender"] = ui->senderName->text();
    currentDeclInfo["senderAddress"] = ui->senderAddress->text();
    currentDeclInfo["senderCountry"] = ui->senderCountry->text();
    currentDeclInfo["senderCounClass"] = QString().number(ui->senderCounClass->currentIndex(),10);

    currentDeclInfo["contract"] = ui->contract->text();
    currentDeclInfo["contractFrom"] = ui->contractFrom->text();
    currentDeclInfo["contractTo"] = ui->contractTo->text();
    currentDeclInfo["specification"] = ui->specification->text();
    currentDeclInfo["specificationFrom"] = ui->specificationFrom->text();
    currentDeclInfo["passport"] = ui->passport->text();

    currentDeclInfo["goodsNumber"] = ui->goodsNumber->text();
    currentDeclInfo["ntwt"] = ui->ntwt->text();
    currentDeclInfo["grwt"] = ui->grwt->text();
    currentDeclInfo["placeNumber"] = ui->placeNumber->text();
    currentDeclInfo["fullwt"] = ui->fullwt->text();

    currentDeclInfo["deliveryTerms"] = ui->deliveryTerms->text();
    currentDeclInfo["termsCombo"] = QString().number(ui->termsCombo->currentIndex(),10);
    currentDeclInfo["invoicePrice"] = ui->invoicePrice->text();
    currentDeclInfo["invCurr"] = QString().number(ui->invCurr->currentIndex(),10);
    currentDeclInfo["trCountry"] = ui->trCountry->text();
    currentDeclInfo["trCClass"] = QString().number(ui->trCClass->currentIndex(),10);
    currentDeclInfo["procCombo"] = QString().number(ui->procCombo->currentIndex(),10);
    currentDeclInfo["DECLORATION_ID"] = currentDecl;

    return currentDeclInfo;
}

void MainWindow::on_updateButton_clicked()
{
    //getCurrentClientInfo();
    qDebug() << "WTF";
    currentClient = myDb.updateClient(getCurrentClientInfo(), currentClient);

    for(int i = 0; i < ui->contactList->rowCount(); i++)
    {
        myDb.updateContact(getCurrentContactInfo(i));
        qDebug() << "WTF" << i;
    }
    updateFrame();

}

void MainWindow::on__orgName_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    ui->_orgName->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on__inn_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    ui->_inn->setStyleSheet("background-color: #e3ffc8");
}

//---------------------------------------
void MainWindow::on_senderName_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->senderName->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_senderAddress_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->senderAddress->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_senderCountry_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->senderCountry->setStyleSheet("background-color: #e3ffc8");
}


void MainWindow::on_destinName_textEdited(const QString &arg1)
{
     Q_UNUSED(arg1); ui->destinName->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_destinInn_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->destinInn->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_destinKpp_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->destinKpp->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_destinOgrn_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->destinOgrn->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_destinAddress_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->destinAddress->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_contract_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->contract->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_contractFrom_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->contractFrom->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_contractTo_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->contractTo->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_specification_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->specification->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_specificationFrom_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->specificationFrom->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_passport_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->passport->setStyleSheet("background-color: #e3ffc8");

}

//void MainWindow::on_goodsNumber_textEdited(const QString &arg1)
//{
//    Q_UNUSED(arg1); ui->goodsNumber->setStyleSheet("background-color: #e3ffc8");
//}

void MainWindow::on_ntwt_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->ntwt->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_grwt_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->grwt->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_placeNumber_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->placeNumber->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_fullwt_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->fullwt->setStyleSheet("background-color: #e3ffc8");

}

void MainWindow::on_deliveryTerms_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->deliveryTerms->setStyleSheet("background-color: #e3ffc8");
}


void MainWindow::on_invoicePrice_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->invoicePrice->setStyleSheet("background-color: #e3ffc8");
}


void MainWindow::on_trCountry_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1); ui->trCountry->setStyleSheet("background-color: #e3ffc8");
}


//--------------------
void MainWindow::on__bank_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    ui->_bank->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on__ogrn_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    ui->_ogrn->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on__cpp_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    ui->_cpp->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on__curAccount_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    ui->_curAccount->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on__jurAdd_textChanged()
{
    ui->_jurAdd->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on__postAdd_textChanged()
{
    ui->_postAdd->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on__prefix_textEdited(const QString &arg1)
{
    Q_UNUSED(arg1);
    ui->_prefix->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_addRowToContact_clicked()
{
    QTableWidgetItem *item;
    QString retId;
    ui->contactList->blockSignals(true);
   if(ui->contactList->rowCount() == 0)
   {
        ui->contactList->setRowCount(ui->contactList->rowCount()+1);
        item = new QTableWidgetItem;
        ui->contactList->setItem(0,0,item);
        item = new QTableWidgetItem;
        ui->contactList->setItem(0,1,item);
        item = new QTableWidgetItem;
        ui->contactList->setItem(0,2,item);
        item = new QTableWidgetItem;
        ui->contactList->setItem(0,3,item);
        item = new QTableWidgetItem;
        ui->contactList->setItem(0,4,item);

        retId = myDb.addContact(getCurrentContactInfo(0));
        item = new QTableWidgetItem;
        item->setText(retId);
        ui->contactList->setItem(0,0,item);
   }
   else
       if(!ui->contactList->item(ui->contactList->rowCount()-1, 0)->text().isNull())
       {
           ui->contactList->setRowCount(ui->contactList->rowCount()+1);
           item = new QTableWidgetItem;
           ui->contactList->setItem(ui->contactList->rowCount()-1,0,item);
           item = new QTableWidgetItem;
           ui->contactList->setItem(ui->contactList->rowCount()-1,1,item);
           item = new QTableWidgetItem;
           ui->contactList->setItem(ui->contactList->rowCount()-1,2,item);
           item = new QTableWidgetItem;
           ui->contactList->setItem(ui->contactList->rowCount()-1,3,item);
           item = new QTableWidgetItem;
           ui->contactList->setItem(ui->contactList->rowCount()-1,4,item);

           retId = myDb.addContact(getCurrentContactInfo(ui->contactList->rowCount()-1));
           item = new QTableWidgetItem;
           item->setText(retId);
           ui->contactList->setItem(ui->contactList->rowCount()-1,0,item);
       }
   ui->contactList->blockSignals(false);
}

void MainWindow::on_contactList_cellChanged(int row, int column)
{
    ui->contactList->item(row, column)->setBackground(QColor("#e3ffc8"));
}

void MainWindow::on_tabWidget_currentChanged(int index)
{
    updateFrame();
}

void MainWindow::on_addOrder_triggered()
{
    Order addOrder;
    addOrder.setModal(false);

    if(addOrder.exec() == QDialog::Accepted)
    {
        myDb.addOrder(addOrder.getOrderInfo());
        updateFrame();

    }
}

void MainWindow::on_goodTitle_textEdited(const QString &arg1)
{
    ui->goodTitle->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_tnVedCode_textEdited(const QString &arg1)
{
    ui->tnVedCode->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_country_textEdited(const QString &arg1)
{
    ui->country->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_registPlace_textEdited(const QString &arg1)
{
    ui->registPlace->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_placeExec_textEdited(const QString &arg1)
{
    ui->placeExec->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_weightNetto_textEdited(const QString &arg1)
{
    ui->weightNetto->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_weightBrutto_textEdited(const QString &arg1)
{
    ui->weightBrutto->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_comment_textChanged()
{
    ui->comment->setStyleSheet("background-color: #e3ffc8");
}

void MainWindow::on_addGoodsButton_clicked()
{

    AddGoods addGo;
    addGo.setModal(false);
    if(addGo.exec() == QDialog::Accepted)
    {
        addGo.setOrderId(currentOrder);
        currentGoodsRow = ui->shortInfoTable->rowCount();
        currentGoods = myDb.addGoods(addGo.getGoodsInfo());
        updateFrame();

    }
}

void MainWindow::on_deleteGoodsButton_clicked()
{
    myDb.dropGoods(currentGoods);
    currentGoods = "";
    currentGoodsRow = 0;
    updateFrame();

}

void MainWindow::on_updateOrderButton_clicked()
{
    //getCurrentClientInfo();

    currentGoods = myDb.updateGoods(getCurrentGoodsInfo());

    for(int i = 0; i < ui->contactList->rowCount(); i++)
    {
        myDb.updateContact(getCurrentContactInfo(i));
        qDebug() << "WTF" << i;
    }

    updateFrame();
}



void MainWindow::on_addDeclButton_clicked()
{

    AddDecloration addDe;
    addDe.setModal(false);
    if(addDe.exec() == QDialog::Accepted)
    {
        addDe.setOrderId(currentOrder);
        currentDeclRow = ui->declTable->rowCount();
        currentDecl = myDb.addDecloration(addDe.getDeclInfo());
        updateFrame();

    }
}

void MainWindow::on_changeDeclButton_clicked()
{
    QRegExp re("(\\d+)");
    int temp = 0;
    for(int i = 0; i < vectGoods.size(); i++)
    {
        re.indexIn(vectGoods[i]->text());
        if(vectGoods[i]->isEnabled())
        {
            if(vectGoods[i]->isChecked())
            {

                myDb.updateGoods(currentDecl, re.cap(1));
                temp++;
            }
            else
            {
                myDb.updateGoods("0", re.cap(1));
            }
        }
    }
    QMap<QString, QString> upCount;
    upCount["DECLORATION_ID"] = currentDecl;
    upCount["goodsCount"] = QString().number(temp, 10);
    myDb.updateDecl(upCount);

}



void MainWindow::on_updateSumInfoButton_clicked()
{
    currentDecl = myDb.updateDecl(getCurrentDeclInfo());

    for(int i = 0; i < ui->preBorderTable->rowCount(); i++)
    {
        myDb.updateBorder(getCurrentBorderInfo(i));
    }

    for(int i = 0; i < ui->preDepartTable->rowCount(); i++)
    {
        myDb.updateDepart(getCurrentDepartInfo(i));
    }

    for(int i = 0; i < ui->transCostsTable->rowCount(); i++)
    {
        myDb.updateTrans(getTransInfo(i));
    }

    for(int i = 0; i < ui->extraPactTable->rowCount(); i++)
    {
        myDb.updatePact(getCurrentPactInfo(i));
    }


    updateFrame();

}

void MainWindow::on_addPreBorderButton_clicked()
{
    QTableWidgetItem *item;
    QString retId;
    ui->preBorderTable->blockSignals(true);
   if(ui->preBorderTable->rowCount() == 0)
   {
        ui->preBorderTable->setRowCount(ui->preBorderTable->rowCount()+1);
        item = new QTableWidgetItem;
        ui->preBorderTable->setItem(0,0,item);
        item = new QTableWidgetItem;
        ui->preBorderTable->setItem(0,1,item);
        item = new QTableWidgetItem;
        ui->preBorderTable->setItem(0,2,item);
        item = new QTableWidgetItem;
        ui->preBorderTable->setItem(0,3,item);
        item = new QTableWidgetItem;
        ui->preBorderTable->setItem(0,4,item);

        retId = myDb.addBorder(getCurrentBorderInfo(0));
        item = new QTableWidgetItem;
        item->setText(retId);
        ui->preBorderTable->setItem(0,0,item);
   }
   else
       if(!ui->preBorderTable->item(ui->preBorderTable->rowCount()-1, 0)->text().isNull())
       {
           ui->preBorderTable->setRowCount(ui->preBorderTable->rowCount()+1);
           item = new QTableWidgetItem;
           ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1,0,item);
           item = new QTableWidgetItem;
           ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1,1,item);
           item = new QTableWidgetItem;
           ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1,2,item);
           item = new QTableWidgetItem;
           ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1,3,item);
           item = new QTableWidgetItem;
           ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1,4,item);

           retId = myDb.addBorder(getCurrentBorderInfo(ui->preBorderTable->rowCount()-1));
           item = new QTableWidgetItem;
           item->setText(retId);
           ui->preBorderTable->setItem(ui->preBorderTable->rowCount()-1,0,item);
       }
   ui->preBorderTable->blockSignals(false);
}

void MainWindow::on_addPreDepartButton_clicked()
{
    QTableWidgetItem *item;
    QString retId;
    ui->preDepartTable->blockSignals(true);
   if(ui->preDepartTable->rowCount() == 0)
   {
        ui->preDepartTable->setRowCount(ui->preDepartTable->rowCount()+1);
        item = new QTableWidgetItem;
        ui->preDepartTable->setItem(0,0,item);
        item = new QTableWidgetItem;
        ui->preDepartTable->setItem(0,1,item);
        item = new QTableWidgetItem;
        ui->preDepartTable->setItem(0,2,item);
        item = new QTableWidgetItem;
        ui->preDepartTable->setItem(0,3,item);
        item = new QTableWidgetItem;
        ui->preDepartTable->setItem(0,4,item);

        retId = myDb.addDepart(getCurrentDepartInfo(0));
        item = new QTableWidgetItem;
        item->setText(retId);
        ui->preDepartTable->setItem(0,0,item);
   }
   else
       if(!ui->preDepartTable->item(ui->preDepartTable->rowCount()-1, 0)->text().isNull())
       {
           ui->preDepartTable->setRowCount(ui->preDepartTable->rowCount()+1);
           item = new QTableWidgetItem;
           ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1,0,item);
           item = new QTableWidgetItem;
           ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1,1,item);
           item = new QTableWidgetItem;
           ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1,2,item);
           item = new QTableWidgetItem;
           ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1,3,item);
           item = new QTableWidgetItem;
           ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1,4,item);

           retId = myDb.addDepart(getCurrentDepartInfo(ui->preDepartTable->rowCount()-1));
           item = new QTableWidgetItem;
           item->setText(retId);
           ui->preDepartTable->setItem(ui->preDepartTable->rowCount()-1,0,item);
       }
   ui->preDepartTable->blockSignals(false);
}

void MainWindow::on_addTransCostsButton_clicked()
{
    QTableWidgetItem *item;
    QString retId;
    ui->transCostsTable->blockSignals(true);
   if(ui->transCostsTable->rowCount() == 0)
   {
        ui->transCostsTable->setRowCount(ui->transCostsTable->rowCount()+1);
        item = new QTableWidgetItem;
        ui->transCostsTable->setItem(0,0,item);
        item = new QTableWidgetItem;
        ui->transCostsTable->setItem(0,1,item);
        item = new QTableWidgetItem;
        ui->transCostsTable->setItem(0,2,item);


        retId = myDb.addTrans(getTransInfo(0));
        item = new QTableWidgetItem;
        item->setText(retId);
        ui->transCostsTable->setItem(0,0,item);
   }
   else
       if(!ui->transCostsTable->item(ui->transCostsTable->rowCount()-1, 0)->text().isNull())
       {
           ui->transCostsTable->setRowCount(ui->transCostsTable->rowCount()+1);
           item = new QTableWidgetItem;
           ui->transCostsTable->setItem(ui->transCostsTable->rowCount()-1,0,item);
           item = new QTableWidgetItem;
           ui->transCostsTable->setItem(ui->transCostsTable->rowCount()-1,1,item);
           item = new QTableWidgetItem;
           ui->transCostsTable->setItem(ui->transCostsTable->rowCount()-1,2,item);

           retId = myDb.addTrans(getTransInfo(ui->transCostsTable->rowCount()-1));
           item = new QTableWidgetItem;
           item->setText(retId);
           ui->transCostsTable->setItem(ui->transCostsTable->rowCount()-1,0,item);
       }
   ui->transCostsTable->blockSignals(false);
}

void MainWindow::on_addEPactButtom_clicked()
{
    QTableWidgetItem *item;
    QString retId;
    ui->extraPactTable->blockSignals(true);
   if(ui->extraPactTable->rowCount() == 0)
   {
        ui->extraPactTable->setRowCount(ui->extraPactTable->rowCount()+1);
        item = new QTableWidgetItem;
        ui->extraPactTable->setItem(0,0,item);
        item = new QTableWidgetItem;
        ui->extraPactTable->setItem(0,1,item);
        item = new QTableWidgetItem;
        ui->extraPactTable->setItem(0,2,item);


        retId = myDb.addPact(getCurrentPactInfo(0));
        item = new QTableWidgetItem;
        item->setText(retId);
        ui->extraPactTable->setItem(0,0,item);
   }
   else
       if(!ui->extraPactTable->item(ui->extraPactTable->rowCount()-1, 0)->text().isNull())
       {
           ui->extraPactTable->setRowCount(ui->extraPactTable->rowCount()+1);
           item = new QTableWidgetItem;
           ui->extraPactTable->setItem(ui->extraPactTable->rowCount()-1,0,item);
           item = new QTableWidgetItem;
           ui->extraPactTable->setItem(ui->extraPactTable->rowCount()-1,1,item);
           item = new QTableWidgetItem;
           ui->extraPactTable->setItem(ui->extraPactTable->rowCount()-1,2,item);

           retId = myDb.addPact(getCurrentPactInfo(ui->extraPactTable->rowCount()-1));
           item = new QTableWidgetItem;
           item->setText(retId);
           ui->extraPactTable->setItem(ui->extraPactTable->rowCount()-1,0,item);
       }
   ui->extraPactTable->blockSignals(false);
}

void MainWindow::on_settings_triggered()
{
    Settings set;
    set.setModal(false);
    if(set.exec() == QDialog::Accepted)
    {
        myDb.setSettings(set.getHost(), set.getName());
        myDb.disconnectWithDataBase();

        updateFrame();
        on_ConnectWithDataBase_triggered();
    }
}

void MainWindow::on_goodsCombo_activated(const QString &arg1)
{
    QRegExp re("(\\d+)");
    re.indexIn(arg1);
    currentOfGoods = re.cap(1);
    updateFrame();
}

void MainWindow::on_pushButton_2_clicked()
{
    currentOfGoods = myDb.updateGoods(getCurrentOfGoodsInfo());
}

void MainWindow::on_pushButton_6_clicked()
{
    on_AddClient_triggered();
}

void MainWindow::on_pushButton_4_clicked()
{
    on_addOrder_triggered();
}
